package com.twotiger.and.activity.account.invested;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import app.twotiger.p2p.R;
import app.twotiger.p2p.R.color;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.twotiger.and.Constants;
import com.twotiger.and.activity.WebViewPage;
import com.twotiger.and.activity.base.BaseActivity;
import com.twotiger.and.activity.base.BaseHandler;
import com.twotiger.and.activity.base.SubTitle;
import com.twotiger.and.adapter.ProjectDetailrepayAdapter;
import com.twotiger.and.bean.Basebean;
import com.twotiger.and.bean.DebtTrans;
import com.twotiger.and.bean.Investment;
import com.twotiger.and.util.ArithUtils;
import com.twotiger.and.util.DateUtil;
import com.twotiger.and.util.ListUtils;
import com.twotiger.and.util.StringUtils;
import com.twotiger.and.util.URLUtil;
import com.twotiger.and.util.ViewUtils;
import com.twotiger.and.view.UnConflictListView;

/**
 * 投资记录详情
 * @Description: TODO(这里描述这个类的作用) 
 * @Author douxy
 * @Date 2015年8月12日 下午2:50:02
 */
public class InverstedRecordDetailPage extends BaseActivity {

    private static final int GETPROJECTEDINFOSUCCES = 0;
    private static final int GETPROJECTEDINFOFAIL = 1;

    private TextView inverst_time;
    private TextView project_code;
    private TextView projct_chinese_name;
    private TextView projct_cycle_time;
    private TextView Inverst_cash;
    private TextView receive_money_value;
    private TextView wait_recieve_text;
    private UnConflictListView invest_detail_list;
    private HashMap<String, String> parm;
    private SubTitle subTitle;
    private DebtTrans mDebtTrans;
    private String investId; //投资记录唯一标识
    private String type; //投资记录的类型  （common 和 wytyj）
    private String mode;
    private LinearLayout mid_debttrans_layout;
    private LinearLayout mid_acive_layout;
    private TextView tv_oldinvest_amount;
    private TextView tv_realmoney;
    private ImageView iv_inverst_complete, iv_inverst_trans;
    private TextView tv_active_info;
    private TextView tv_inverst_trans;

    @Override
    protected void processLogic() {
        parm = getParm();
        investId = getIntent().getStringExtra("id");
        type = getIntent().getStringExtra("type");
        mode = getIntent().getStringExtra("mode");
        //        if(Constants.TYPE_NID_HEIDONG.equals(type)){
        //            subTitle.title_right_tv.setVisibility(View.GONE);
        //        }else{
        //            subTitle.title_right_tv.setVisibility(View.VISIBLE);
        //        }
        initdata();
    }

    private void initdata() {
        getinverstproject();
    }

    public void getinverstproject() {
        parm.clear();
        parm.put("token", getToken());
        parm.put("investId", investId);
        parm.put("type", type);
        parm.put("mode", mode);
        getDate(parm, Constants.INVESTDETAILQUERY, handler, GETPROJECTEDINFOSUCCES,
            GETPROJECTEDINFOFAIL, true, false, false);
    }

    @Override
    protected void setListener() {
        handler = new BaseHandler(this) {

            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                case GETPROJECTEDINFOSUCCES:
                    Basebean result = JSONObject.parseObject((String) msg.obj, Basebean.class);
                    if (result.isOk()) {
                        mDebtTrans = JSON.parseObject(result.data, DebtTrans.class);
                        //根据ptype字段的返回是否为空，来判断协议是否显示
                        if (StringUtils.isEmpty(mDebtTrans.getpType())) {
                            subTitle.title_right_tv.setVisibility(View.GONE);
                        } else {
                            subTitle.title_right_tv.setVisibility(View.VISIBLE);
                        }
                        inverst_time.setText(DateUtil.getDateStringForLong(Long.valueOf(mDebtTrans.getCtime()), DateUtil.DATE_FMT));
                        project_code.setText(mDebtTrans.getNid());
                        projct_chinese_name.setText(mDebtTrans.getName());
                        projct_cycle_time.setText(mDebtTrans.getPeriod() + "");
                        Inverst_cash
                            .setText(ArithUtils.coverMoneyComma(mDebtTrans.getAmount() + ""));
                        receive_money_value.setText(ArithUtils.coverMoneyComma(mDebtTrans
                            .getYield() + "")
                                                    + "");
                        wait_recieve_text.setText(ArithUtils.coverMoneyComma(mDebtTrans
                            .getInterest() + ""));

                        if (1 == mDebtTrans.getAssignMsg()) {
                            mid_debttrans_layout.setVisibility(View.VISIBLE);
                            tv_oldinvest_amount.setText(ArithUtils.coverMoneyComma(mDebtTrans
                                .getOldAmount() + ""));
                            tv_realmoney.setText(ArithUtils.coverMoneyComma(mDebtTrans
                                .getDealProceeds() + ""));
                        } else if (0 == mDebtTrans.getAssignMsg()) {
                            mid_debttrans_layout.setVisibility(View.GONE);
                        }
                        if ("1".equals(mDebtTrans.getAssignMark())) { //已转让
                            iv_inverst_trans.setVisibility(View.GONE);
                            iv_inverst_trans.setImageResource(R.drawable.invest_transe_yi);
                        } else if ("2".equals(mDebtTrans.getAssignMark())) { //有转让
                            iv_inverst_trans.setVisibility(View.GONE);
                            iv_inverst_trans.setImageResource(R.drawable.invest_transe_you);
                        } else if ("0".equals(mDebtTrans.getAssignMark())) { //无
                            iv_inverst_trans.setVisibility(View.GONE);
                        }
                        if ("1".equals(mDebtTrans.getAssignMark())) { //已转让
                            tv_inverst_trans.setVisibility(View.VISIBLE);
                            tv_inverst_trans.setText("已转让");
                        } else if ("2".equals(mDebtTrans.getAssignMark())) { //有转让
                            tv_inverst_trans.setVisibility(View.VISIBLE);
                            tv_inverst_trans.setText("有转让");
                        } else if ("0".equals(mDebtTrans.getAssignMark())) { //无
                            tv_inverst_trans.setVisibility(View.GONE);
                        }
                        

                        if (StringUtils.isEmpty(mDebtTrans.getActivityMsg())) {
                            mid_acive_layout.setVisibility(View.GONE);
                        } else {
                            mid_acive_layout.setVisibility(View.VISIBLE);
                            tv_active_info.setText(mDebtTrans.getActivityMsg());
                        }
                        //判断是否显示已还款图标
                        if ("1".equals(mDebtTrans.getCompleteMark())) {
                            iv_inverst_complete.setVisibility(View.GONE);
                        } else {
                            iv_inverst_complete.setVisibility(View.GONE);
                        }

                        List<Investment> list = JSON.parseArray(mDebtTrans.getList(),
                            Investment.class);
                        if (!ListUtils.isEmpty(list)) {
                            invest_detail_list
                                .setAdapter(new ProjectDetailrepayAdapter(
                                    mContext,
                                    list,
                                    getIntent().getStringExtra("isCyOrYh") == null ? InverstedRecordPage.REQTYPE_CYZ
                                        : getIntent().getStringExtra("isCyOrYh")));
                        }
                    }
                    break;
                case GETPROJECTEDINFOFAIL:
                    break;
                default:
                    break;
                }
                super.handleMessage(msg);
            }
        };

        subTitle.title_right_tv.setOnClickListener(this);
    }

    @Override
    protected void findViewById(View view) {
        inverst_time = (TextView) view.findViewById(R.id.inverst_time);
        project_code = (TextView) view.findViewById(R.id.project_code);
        projct_chinese_name = (TextView) view.findViewById(R.id.projct_chinese_name);
        projct_cycle_time = (TextView) view.findViewById(R.id.projct_cycle_time);
        Inverst_cash = (TextView) view.findViewById(R.id.Inverst_cash);
        receive_money_value = (TextView) view.findViewById(R.id.receive_money_value);
        wait_recieve_text = (TextView) view.findViewById(R.id.wait_recieve_text);
        invest_detail_list = (UnConflictListView) view.findViewById(R.id.invest_detail_list);
        invest_detail_list.setFocusable(false); //不让listview获取焦点，保证页面在最顶部

        mid_debttrans_layout = (LinearLayout) view.findViewById(R.id.mid_debttrans_layout);
        mid_acive_layout = (LinearLayout) view.findViewById(R.id.mid_acive_layout);
        tv_active_info = (TextView) view.findViewById(R.id.tv_active_info);
        tv_oldinvest_amount = (TextView) view.findViewById(R.id.tv_oldinvest_amount);
        tv_realmoney = (TextView) view.findViewById(R.id.tv_realmoney);

        iv_inverst_complete = (ImageView) view.findViewById(R.id.iv_inverst_complete);
        iv_inverst_trans = (ImageView) view.findViewById(R.id.iv_inverst_trans);
        tv_inverst_trans = (TextView) view.findViewById(R.id.tv_inverst_trans);

        subTitle = new SubTitle(view) {
            @Override
            protected void clickTitle(View v) {
            }

            @Override
            protected void clickRightBtn(View v) {
            }

            @Override
            protected void cickLeftBtn(View v) {
                onBackPress();
            }
        };
        subTitle.hideBtns();
        subTitle.setTitle("已投项目详情");
        subTitle.hideBottomLine();
        tintManager.setTintResource(R.drawable.bg_account_statusbar);
        subTitle.setBgResource(R.drawable.bg_account_title);
        subTitle.setColor(getResources().getColor(color.white));
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
        subTitle.title_left_bt.setImageResource(R.drawable.back_w);
        subTitle.title_left_bt.setVisibility(View.VISIBLE);
        subTitle.title_right_tv.setText("协议");
        subTitle.title_right_tv.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onBackPress() {
        finish();
        overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);

    }

    @Override
    protected View loadViewLayout(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.inverst_project_detail, null);
        return view;
    }

    /**
     * 获取token
     * @return
     */

    public String getToken() {
        return application.getUser().token;
    }

    @Override
    public void onClick(View view) {

        if (view == null) {
            return;
        }

        if (mDebtTrans == null) {
            return;
        }

        switch (view.getId()) {
        case R.id.sub_title_right_tv:
            if (ViewUtils.isFastDoubleClick(view)) {
                return;
            }
            if (mDebtTrans == null) { //保证点击的时候invest不为空，要不报错
                return;
            }
            Intent in = new Intent(mContext, WebViewPage.class);
            in.putExtra("title", "合同");
            Map<String, String> contractmap = new HashMap<String, String>();
            contractmap.put("pType", mDebtTrans.getpType());
            contractmap.put("investId", investId);
            contractmap.put("token", getToken());
            in.putExtra(
                "url",
                Constants.HOST + Constants.CONTRACT + "contractHtml?"
                        + URLUtil.createContractUrl(contractmap, "3.0"));
            startNewActivity(in, R.anim.push_left_in, R.anim.push_left_out, false);
            break;

        default:
            break;
        }
    }

}
